package com.csw.shuanfa.GlobalConfig.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
    private Boolean flag;// 是否成功
    private Integer code;// 返回码
    private String msg;// 返回信息
    private Object data;// 返回数据

    private String requestId;

    public static <T> Result<T> ok() {
        return ok(true, StatusCode.OK, ResultInfoEnum.OK.getMsg(), null);
    }

    public static <T> Result<T> ok(T datas) {
        return ok(true, StatusCode.OK, ResultInfoEnum.OK.getMsg(), datas);
    }

    public static <T> Result<T> ok(String msg, T datas) {
        return ok(true, StatusCode.OK, msg, datas);
    }


    private static <T> Result<T> ok(boolean b, Integer code, String msg, Object data) {
        Result<T> r = new Result<>();
        r.flag = b;
        r.code = code;
        r.msg = msg;
        r.data = data;
        return r;
    }

    //#######################################################################################################
    public static <T> Result<T> err() {
        return err(false, StatusCode.ERROR, ResultInfoEnum.ERROR.getMsg(), null);
    }

    public static <T> Result<T> err(T datas) {
        return err(false, StatusCode.ERROR, ResultInfoEnum.ERROR.getMsg(), datas);
    }

    public static <T> Result<T> err(String msg, T datas) {
        return err(false, StatusCode.ERROR, msg, datas);
    }

    /**
     * 权限不足或者登录有问题等其他错误调用
     *
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Result<T> err(Integer code, String msg) {
        return err(false, code, msg, null);
    }

    private static <T> Result<T> err(boolean b, Integer code, String msg, Object data) {
        Result<T> r = new Result<>();
        r.flag = b;
        r.code = code;
        r.msg = msg;
        r.data = data;
        return r;
    }


}
